﻿using Acme.SmartWMS.Common;
using Acme.SmartWMS.Location.DefinitionService.DTO;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic.Core;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace Acme.SmartWMS.Location.DefinitionService
{
    public class DefinitionAppService:ApplicationService,IDefinitionAppService
    {
        /// <summary>
        /// 仓库定义仓储
        /// </summary>
        private readonly IRepository<Definition,int> _definitionRepository;
        public DefinitionAppService(IRepository<Definition,int> definitionRepository)
        {
            _definitionRepository = definitionRepository;
        }
        /// <summary>
        /// 获取仓库定义分页 显示 查询
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="warehouseCode"></param>
        /// <param name="warehouseName"></param>
        /// <returns></returns>
        public async Task<PagedResult> GetDefinitionList(int pageIndex,int pageSize,string ? warehouseCode,string ? warehouseName)
        {
            var predicate = PredicateBuilder.New<Definition>(true);
            if (!string.IsNullOrEmpty(warehouseCode))
            {
                predicate=predicate.And(x=>x.WarehouseCode.Contains(warehouseCode));
            }
            if (!string.IsNullOrEmpty(warehouseName))
            {
                predicate = predicate.And(x => x.WarehouseName.Contains(warehouseName));
            }
            var dto = await _definitionRepository.GetListAsync(predicate);
            PagedResult result = new PagedResult();
            result.PageCount = (int)Math.Ceiling((decimal)(dto.Count == 0 ? 1 : dto.Count) / pageSize);
            result.CurrentPage = pageIndex;
            result.PageSize = pageSize;
            result.RowCount=dto.Count;
            result.Queryable=dto.Skip((pageIndex-1)*pageSize).Take(pageSize).AsQueryable();
            return result;
        }
        /// <summary>
        /// 添加仓库定义
        /// </summary>
        /// <param name="definitionDTO"></param>
        /// <returns></returns>
        public async Task<int> InsertDefinition(DefinitionDTO definitionDTO)
        {
            var warehouseCode = _definitionRepository.CountAsync(x => x.WarehouseCode == definitionDTO.WarehouseCode).Result;
            if (warehouseCode > 0)
            {
                return -1;
            }
            var definition = definitionDTO.MapTo<Definition>();
            await _definitionRepository.InsertAsync(definition);
            return 1;
        }
        /// <summary>
        /// 删除仓库定义
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public async Task<int> DeleteDefinition(string ids)
        {
            string[] arry=ids.Split(',');
            await  _definitionRepository.DeleteAsync(x => ids.Contains(x.Id. ToString()));
            return 1;
        }
        /// <summary>
        /// 获取仓库定义主键Id
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<Definition> GetDefinitionById(int id)
        {
            var definition=await _definitionRepository.GetAsync(x=>x.Id == id);
            var definitionDTO=definition.MapTo<Definition>();
            return definitionDTO;
        }
        /// <summary>
        /// 更新仓库定义
        /// </summary>
        /// <param name="definitionDTO"></param>
        /// <returns></returns>
        public async Task<int> UpdateDefinition(DefinitionDTO definitionDTO)
        {
            var warehouseCode =_definitionRepository.CountAsync(x=>x.WarehouseCode==definitionDTO.WarehouseCode).Result;
            if(warehouseCode > 0)
            {
                return -1;
            }
            var definition = definitionDTO.MapTo<Definition>();
            await _definitionRepository.UpdateAsync(definition);
            return 1;
        }
    }
}
